{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Audio loaders and metadata"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loading audio\n",
    "\n",
    "Essentia relies on the [FFmpeg](https://en.wikipedia.org/wiki/FFmpeg) library for audio input/output, and therefore there are many possibilities when it comes to loading audio. See [AudioLoader](https://essentia.upf.edu/reference/std_AudioLoader.html), [MonoLoader](https://essentia.upf.edu/reference/std_MonoLoader.html), and [EasyLoader](https://essentia.upf.edu/reference/std_EasyLoader.html) algorithms for more details.\n",
    "\n",
    "Below are some examples of their usage loading audio files:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import essentia.standard as es\n",
    "\n",
    "filename = 'audio/dubstep.flac'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(302085,)\n"
     ]
    }
   ],
   "source": [
    "# Load the whole file in mono\n",
    "audio = es.MonoLoader(filename=filename)()\n",
    "print(audio.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(302085, 2)\n"
     ]
    }
   ],
   "source": [
    "# Load the whole file in stereo\n",
    "audio, _, _, _, _, _ = es.AudioLoader(filename=filename)()\n",
    "print(audio.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(109600,)\n"
     ]
    }
   ],
   "source": [
    "# Load and resample to 16000 Hz\n",
    "audio = es.MonoLoader(filename=filename, sampleRate=16000)()\n",
    "print(audio.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(441000,)\n"
     ]
    }
   ],
   "source": [
    "# Load only a 10-seconds segment in mono, starting from the 2nd minute\n",
    "audio = es.EasyLoader(filename='audio/Vivaldi_Sonata_5_II_Allegro.flac', \n",
    "                      sampleRate=44100, startTime=60, endTime=70)()\n",
    "print(audio.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Reading file metadata\n",
    "\n",
    "Essentia also supports loading the metadata embedded in audio files (such as [ID3](https://en.wikipedia.org/wiki/ID3)) using [MetadataReader](https://essentia.upf.edu/documentation/reference/std_MetadataReader.html)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('Stubb (a Dub)',\n",
       " 'Mr. Bungle',\n",
       " 'Mr. Bungle',\n",
       " '',\n",
       " 'Avantgarde Metal',\n",
       " '6/10',\n",
       " '1991-08-13',\n",
       " <essentia.common.Pool at 0x7fcfd08aa390>,\n",
       " 439,\n",
       " 160,\n",
       " 44100,\n",
       " 2)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Replace with your own file\n",
    "es.MetadataReader(filename='audio/Mr. Bungle - Stubb (a Dub).mp3')()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The output contains standard metadata fields (track name, artist, name, album name, track number, etc.) as well as bitrate and samplerate. It also includes an Essentia pool object containing all other fields found:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "metadata.tags.album ['Mr. Bungle']\n",
      "metadata.tags.albumartist ['Mr. Bungle']\n",
      "metadata.tags.albumartistsort ['Mr. Bungle']\n",
      "metadata.tags.artist ['Mr. Bungle']\n",
      "metadata.tags.artists ['Mr. Bungle']\n",
      "metadata.tags.artistsort ['Mr. Bungle']\n",
      "metadata.tags.asin ['B000002LPW']\n",
      "metadata.tags.barcode ['075992664028']\n",
      "metadata.tags.catalognumber ['9 26640-2']\n",
      "metadata.tags.composer ['Mr. Bungle']\n",
      "metadata.tags.date ['1991-08-13']\n",
      "metadata.tags.discnumber ['1/1']\n",
      "metadata.tags.genre ['Avantgarde Metal']\n",
      "metadata.tags.isrc ['USWB19903997']\n",
      "metadata.tags.label ['Warner Bros. Records']\n",
      "metadata.tags.media ['CD']\n",
      "metadata.tags.musicbrainz album release country ['US']\n",
      "metadata.tags.musicbrainz album status ['official']\n",
      "metadata.tags.musicbrainz album type ['album']\n",
      "metadata.tags.musicbrainz release track id ['64236797-30a7-3dab-8398-eea09e92218d']\n",
      "metadata.tags.musicbrainz_albumartistid ['277e21a9-2d64-452d-96c4-2d23a7af5891']\n",
      "metadata.tags.musicbrainz_albumid ['6e2a6ac8-e3b0-4992-b107-3968f2ddcad6']\n",
      "metadata.tags.musicbrainz_artistid ['277e21a9-2d64-452d-96c4-2d23a7af5891']\n",
      "metadata.tags.musicbrainz_releasegroupid ['8033ef41-a96d-30e0-8e98-f2e17f59db5f']\n",
      "metadata.tags.musicbrainz_trackid ['af930c80-3c0b-4d60-9b70-44a8a7b1cc32']\n",
      "metadata.tags.originaldate ['1991']\n",
      "metadata.tags.originalyear ['1991']\n",
      "metadata.tags.script ['Latn']\n",
      "metadata.tags.title ['Stubb (a Dub)']\n",
      "metadata.tags.tracknumber ['6/10']\n"
     ]
    }
   ],
   "source": [
    "metadata_pool = es.MetadataReader(filename='audio/Mr. Bungle - Stubb (a Dub).mp3')()[7]\n",
    "for d in metadata_pool.descriptorNames():\n",
    "    print(d, metadata_pool[d])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
